package com.qire.manhua.activity;

import android.os.Bundle;

import com.qire.antsbinder.dal.exception.DataAccessException;
import com.qire.antscore.annotation.DalExceptionObserve;
import com.qire.antscore.annotation.RouteMapping;
import com.qire.antsrouter.AntsRouter;
import com.qire.antsrouter.card.FloorCard;
import com.qire.antsrouter.inject.Autowired;
import com.qire.antsrouter.lifecycleHandler.ActivityManageHandler;
import com.qire.common.InitManage;
import com.qire.common.constant.Url;
import com.qire.common.support.base.BaseActivity;
import com.qire.common.constant.Navigation;
import com.qire.common.inject.EventControllerInjector;
import com.qire.common.inject.ViewModelInjector;
import com.qire.common.utils.ToastHelper;
import com.qire.manhua.R;
import com.qire.manhua.controller.MainController;
import com.qire.manhua.dal.service.SyncServerConfigService;
import com.qire.manhua.dal.service.SyncUserDataService;
import com.qire.manhua.dal.service.TeensModeService;
import com.qire.manhua.databinding.ActivityMainBinding;
import com.qire.manhua.dialog.UserAgreementDialog;
import com.qire.manhua.model.viewModel.impl.MainViewModel;

import androidx.annotation.Nullable;

/**
 * 主入口页，进场页面。
 */
@RouteMapping(path = Navigation.Page.MainPage)
public class MainActivity extends BaseActivity<ActivityMainBinding> {

    @Autowired(injector = ViewModelInjector.class)
    MainViewModel viewModel;

    @Autowired(injector = EventControllerInjector.class)
    MainController controller;

    private volatile boolean isClose = true;

    @Override
    public int layoutRid() {
        return R.layout.activity_main;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        viewModel.addCloseStateObserver(this::closePage);

        if(UserAgreementDialog.checkUserAgreementState()) {
            agreeAuthorize();
        } else {
            UserAgreementDialog userAgreementDialog = new UserAgreementDialog(this);
            userAgreementDialog.setAgreeAction(this::agreeAuthorize);
            userAgreementDialog.setDisagreeAction(this::exitApp);
            userAgreementDialog.postShow();
        }
    }

    private void agreeAuthorize() {
        InitManage.init();
        new SyncServerConfigService().syncConfig();
        new SyncUserDataService().refreshUserDetailData();
        new TeensModeService().queryTeensModeState();
        viewModel.playAd();
    }

    boolean mIsExit = false;
    public void exitApp() {
        if(mIsExit) {
            ActivityManageHandler.HANDLER.exit();
        } else {
            mIsExit = true;
//            new Handler().postDelayed(()-> mIsExit = false, 2000);
            ToastHelper.showToast("不同意将无法使用我们的产品及服务，并会退出APP");
        }
    }

    public synchronized void closePage(boolean state) {
        if(state && isClose) {
            isClose = false;
            AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.HomePage).navigation();
            finish();
        }
    }

    @DalExceptionObserve(tags = {Url.v3_advertise})
    public void thirdPartyLoginExceptionHandle(DataAccessException e) {
        viewModel.normalEnter();
    }
}
